<?php

class apidefinition_ActionController extends Zend_Controller_Action
{
  function init() {
  	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    $this->getHelper('viewRenderer')->setNoRender();
  }

  function createAction() {
    require_once('apidefinition/objects/action.php');
    try{
	  $results = APIDefinition_Object_Action::create($this->_getAllParams());
    } catch(APIDefinition_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
	$data['status'] = 	'OK';
	echo Zend_Registry::get('dt_serializer')->serialize($data);;
    return;
  }
  
  
  function getAction() {
    require_once('apidefinition/objects/action.php');
    try {
      $action = new APIDefinition_Object_Action($this->_getParam('name'),$this->_getParam('controllerName'),$this->_getParam('moduleName'));
      $resAction = $action->asArray();
    } catch(APIDefinition_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
    
    //We list the different actions of the controller
    //require_once('apidefinition/objects/controller.php');
    //$resControllers = APIDefinition_Object_Controller::listing($module->getName());
    
	$data['status'] = 	'OK';
	$data['action'] = $resAction;
	
	//We get also the last published version of the message
    require_once('apidefinition/objects/message.php');
    try{
      $message = new APIDefinition_Object_Message($this->_getParam('name'),$this->_getParam('controllerName'),$this->_getParam('moduleName'),'','current');
      $resMessage = $message->asArray();
    } catch (APIDefinition_Object_Exception $e) {
      $data['message']['status'] = 'KO';
      $data['message']['error']['num'] = 	$e->getCode();
      $data['message']['error']['message'] = 	$e->getMessage();      	
    }
    if(isset($resMessage) && !empty($resMessage)) {
      $data['message'] = $resMessage;	
      $data['message']['status'] = 'OK';
    }
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;  	
  }
  
  function gethistoryAction() {
  	require_once('apidefinition/objects/action.php');
    try {
      $action = new APIDefinition_Object_Action($this->_getParam('name'),$this->_getParam('controllerName'),$this->_getParam('moduleName'));
      $resAction = $action->asArray();
    } catch(APIDefinition_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
    
    //We list the different actions of the controller
    //require_once('apidefinition/objects/controller.php');
    //$resControllers = APIDefinition_Object_Controller::listing($module->getName());
    
	$data['status'] = 	'OK';
	$data['action'] = $resAction;
	
	//We get the ddata of history for the message
    try{
      $historyList = $action->getHistory();
    } catch (APIDefinition_Object_Exception $e) {
      $data['message']['status'] = 'KO';
      $data['message']['error']['num'] = 	$e->getCode();
      $data['message']['error']['message'] = 	$e->getMessage();      	
    }
    if(isset($historyList) && !empty($historyList)) {
      $data['history'] = $historyList;	
    }
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;  	

  }

} //End class


?>